package admin.ui;

import game.*;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

import admin.exceptions.*;

import messages.InvalidInvitationReceivedMessage.ErrorCode;
import connect4client.core.AbstractGUIMediator;
import connect4client.core.Controller;
import core.PluginInfo;

public class GUIMediatorImpl extends AbstractGUIMediator {

	public GUIMediatorImpl(Controller controller) {
		super(controller);
	}

	public void chat(String login, String contents, int matchId) { }
	public void giveUp(int matchId) { }
	public void invalidMove(int matchId) { }
	public void invitationReceived(String opponentName, String gameName, int replyId) { }
	public void invitationTimedOut(String login, String gameType) { }
	public void registrationFailed() { }
	public void registrationSuccessful() { }
	public void userLoggedOut(String playerLogin) { }
	public void wall(String senderLogin, String contents) { }
	public void invalidInvitationReceived(ErrorCode errorCode) { }
	public void invitationAccepted(Player opponent, String gameName) { }
	public void invitationDenied(Player opponent, String gameName) { }
	public void matchEnded(int matchId, List<Player> players, String winner) { }
	public void matchEndedTied(int matchId, List<Player> players) { }
	public void matchStarted(Player me, Player opponent, String gameName, int matchId, String nextPlayer) { }
	public void playerMove(PlayerMove playerMove, int matchId, String nextPlayer) { }


	@SuppressWarnings("unchecked")
	@Override
	public void loginFailed() {
		queue.offer(false);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void loginSuccessful() {
		queue.offer(true);
	}
	

	@SuppressWarnings("unchecked")
	public void pluginShutdownFailed(String adminLogin, String pluginName) {
		if (adminLogin.equals(controller.getUser().getLogin()))
			queue.offer(false);
	}

	
	@SuppressWarnings("unchecked")
	public void pluginShutdownSuccessful(String adminLogin, String pluginName) {
		if (adminLogin.equals(controller.getUser().getLogin()))
			queue.offer(true);
	}

	
	@SuppressWarnings("unchecked")
	public void pluginStartupFailed(String adminLogin, String pluginName) {
		if (adminLogin.equals(controller.getUser().getLogin()))
			queue.offer(false);
	}

	
	@SuppressWarnings("unchecked")
	public void pluginStartupSuccessful(String adminLogin, String pluginName) {
		if (adminLogin.equals(controller.getUser().getLogin()))
			queue.offer(true);
	}


	private boolean firstTime = true;
	
	@SuppressWarnings("unchecked")
	public void listPlayersReply(List<Player> players) {
		if (firstTime) {
			firstTime = false;
		} else {
			queue.offer(players);
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public void listMatchsReply(List<MatchStatistics> activeMatchs) {
		queue.offer(activeMatchs);
	}

	
	@SuppressWarnings("unchecked")
	public void listPluginsReply(Collection<PluginInfo> plugins) {
		queue.offer(plugins);
	}

	

	@Override
	public void userLoggedIn(Player player) {
		// TODO Auto-generated method stub
		
	}

	
	@SuppressWarnings("unchecked")
	public void accountCreationSuccessful(String adminLogin, String login) {
		if (adminLogin.equals(controller.getUser().getLogin()))
			queue.offer(true);
	}
	
	
	@SuppressWarnings("unchecked")
	public void accountCreationFailed(String adminLogin, String login, messages.AccountCreationFailedMessage.ErrorCode errorCode) {
		if (adminLogin.equals(controller.getUser().getLogin())) {
			if (errorCode == messages.AccountCreationFailedMessage.ErrorCode.InsufficientPermissions)
				queue.offer(new InsufficientPermissionsException("Create account" + login));
			else if (errorCode == messages.AccountCreationFailedMessage.ErrorCode.InvalidParameters) {
				queue.offer(new InvalidRoleException());
			} else {
				queue.offer(new UserAlreadyExistsException(login));
			}
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public void accountEditionSuccessful(String adminLogin, String login) {
		if (adminLogin.equals(controller.getUser().getLogin()))
			queue.offer(true);
	}
	

	@SuppressWarnings("unchecked")
	public void accountEditionFailed(String adminLogin, String login, messages.AccountEditionFailedMessage.ErrorCode errorCode) {
		if (adminLogin.equals(controller.getUser().getLogin())) {
			if (errorCode == messages.AccountEditionFailedMessage.ErrorCode.InsufficientPermissions)
				queue.offer(new InsufficientPermissionsException("Edit account" + login));
			else if (errorCode == messages.AccountEditionFailedMessage.ErrorCode.InvalidParameters) {
				queue.offer(new InvalidRoleException());
			} else {
				queue.offer(new UnknownUserException(login));
			}
		}
	}
	

	@SuppressWarnings("unchecked")
	public void accountRemotionSuccessful(String adminLogin, String login) {
		if (adminLogin.equals(controller.getUser().getLogin()))
			queue.offer(true);
	}
	
	
	@SuppressWarnings("unchecked")
	public void accountRemotionFailed(String adminLogin, String login, messages.AccountRemotionFailedMessage.ErrorCode errorCode) {
		if (adminLogin.equals(controller.getUser().getLogin())) {
			if (errorCode == messages.AccountRemotionFailedMessage.ErrorCode.InsufficientPermissions) {
				queue.offer(new InsufficientPermissionsException("Delete account" + login));
			} else {
				queue.offer(new UnknownUserException(login));
			}
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public void userDetailsReply(Player player, List<MatchStatistics> oldMatchs, List<MatchStatistics> activeMatchs) {
		queue.offer(new Object[] { player, oldMatchs, activeMatchs });
	}
	
	
	@SuppressWarnings("unchecked")
	public void invalidUserDetailsRequest(String login, messages.InvalidUserDetailsRequestMessage.ErrorCode errorCode) {
		queue.offer(new UnknownUserException(login));
	}
	

	public Object take() {
		try {
			return queue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
			return e;
		}
	}

	
	private BlockingQueue queue = new LinkedBlockingQueue();
}
